package com.sanjiahantang.youjiarole.module.match.presenters;


import android.app.Activity;
import android.content.Context;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiarole.base.BaseMvpPresenter;
import com.sanjiahantang.youjiarole.base.CallBack;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.module.match.adapters.MatchAdapter;
import com.sanjiahantang.youjiarole.module.match.adapters.MatchListAdapter;
import com.sanjiahantang.youjiarole.module.match.beans.MatchBean;
import com.sanjiahantang.youjiarole.module.match.beans.MatchListBean;
import com.sanjiahantang.youjiarole.module.match.models.IMatchModel;
import com.sanjiahantang.youjiarole.module.match.models.impl.MatchModel;
import com.sanjiahantang.youjiarole.module.match.views.MatchView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zy on 2017/9/14.
 */

public class MatchPresenter extends BaseMvpPresenter<MatchView> {

    private IMatchModel matchModel;
    private MatchView.PullListener pullListener;
    private MatchView.MatchDetailListener matchDetailListener;
    private MatchAdapter matchDetailAdapter;

    public MatchPresenter(){
        matchModel = new MatchModel();
    }

    public void loadMatchList(Context context) {
        Map<String,Object> param = new HashMap<>();
        param.put(KEY_PAGE,0);
        param.put(KEY_PAGE_SIZE,DEFAULT_PAGE_SIZE);
        Map<String,String> map = new HashMap<>();
        param.put(KEY_PARAM,map);
        mView.showProgress();
        matchModel.loadMatchList(context,param,new LoadMatchListCallBack());
    }

    public void setPullListener(MatchView.PullListener pullListener) {
        this.pullListener = pullListener;
    }

    public void setMatchDetailListener(MatchView.MatchDetailListener matchDetailListener) {
        this.matchDetailListener = matchDetailListener;
    }

    public void loadMatchDetail(Context context, int id) {
        Map<String,String> param = new HashMap<>();
        param.put(MatchView.KEY_MATCH_ID,String.valueOf(id));
        if (ConfigValue.DATA_KEY != null){
            param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        }
        mView.showProgress();
        matchModel.loadMatchDetail(context,param,new LoadMatchDetail());
    }

    private class LoadMatchListCallBack implements CallBack<MatchListBean>{

        @Override
        public void onSuccess(MatchListBean response, Context context, Object tag) {
            mView.hideProgress();
            MatchListAdapter adapter = new MatchListAdapter(context,response.getData());
            pullListener.pullToRefresh(adapter);
        }

        @Override
        public void onFailure(String message) {
            mView.hideProgress();
        }
    }

    private class LoadMatchDetail implements CallBack<MatchBean>{

        @Override
        public void onSuccess(MatchBean response, Context context, Object tag) {
            mView.hideProgress();
            if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                matchDetailAdapter = new MatchAdapter(context,response.getData());
                matchDetailListener.loadData(matchDetailAdapter);
            }else{
                Utils.showToast(context,"未找到搭配");
                ((Activity)context).finish();
            }
        }

        @Override
        public void onFailure(String message) {
            mView.hideProgress();
        }
    }
}
